﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BluePortal.Framework;
using BluePortal.Medias.Core;

namespace BluePortal.Medias.Core
{
    public class BCComment : AbstractBusinessComponent
    {
        #region ICommentService
        public void Create(Comment comment)
        {
            AssertNotNull(comment, "comment");
            if (comment.CID == Guid.Empty) comment.CID = Guid.NewGuid();
            if (comment.ID != Guid.Empty)
            {
                Context.Comments.InsertOnSubmit(comment);
                Context.SubmitChanges();
            }
        }

        public void Update(Comment comment)
        {
            AssertNotNull(comment, "comment");
            Comment dbComment = Context.Comments.Single<Comment>(vc => vc.CID == comment.CID);
            dbComment.Text = comment.Text;
            dbComment.CreationDate = comment.CreationDate;
            Context.SubmitChanges();
        }

        public void Delete(Comment comment)
        {
            AssertNotNull(comment, "comment");
            Comment dbComment = Context.Comments.Single<Comment>(vc => vc.CID == comment.CID);
            Context.Comments.DeleteOnSubmit(dbComment);
            Context.SubmitChanges();
        }

        public Comment FindByID(Guid id)
        {
            return Context.Comments.Single<Comment>(c => c.CID == id);
        }

        public FindResult<Comment> FindByMedia(Guid VideoID, FindParameters f)
        {
            return ToFindResult<Comment>(QueryFindByMedia(VideoID), f);
        }

        #endregion

        #region Internal Methods
        public IQueryable<Comment> QueryFindByMedia(Guid VideoID)
        {
            var query = from c in Context.Comments
                        where c.ID == VideoID
                        orderby c.CreationDate descending
                        select c;
            return query;
        }
        #endregion
    }
}
